package com.yao.component.circle;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yao.component.circle.dataobject.Post;
import com.yao.component.circle.dataobject.Message;
import com.yao.sdk.util.Utils;

/**
 * Circle列表的数据适配器
 */
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder> {
    private Post data = new Post();

    private CircleView circleView;
    // 评论或者回复里的可点击的姓名是否被点击
    private boolean isClicked = false;

    private int colorResId = Color.parseColor("#576b95"); // 设置默认文本超链接颜色

    public void setCircleView(CircleView circleView) {
        this.circleView = circleView;
    }

    /**
     * 通过ViewHolder可以访问每个item的所有控件
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {
        //绑定每个CircleItem的控件引用到ViewHolder
        public TextView content;


        public ViewHolder(View v) {
            super(v);
            content = (TextView) v.findViewById(R.id.content);

        }
    }

    // 创建每个Item的视图 (invoked by the item_circle manager)
    @Override
    public MessageAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                        int viewType) {
        //创建circleItem所对应的控件
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_comment, parent, false);
        // set the view's size, margins, paddings and item_circle parameters
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    // Replace the contents of a view (invoked by the item_circle manager)
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        final Message message = data.getMessageList().get(position);
        message.setIndex(position);

        configComment(holder.content, message);

        holder.content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isClicked) {
                    isClicked = false;
                    return;
                }
                // 设置当前所点击的评论在数据源里的Index
                MessageEditor.getInstance().setCircleItemIndex(data.getIndex());
                MessageEditor.getInstance().setMessageItemIndex(position);

                //判断回复人或者评论人是不是自己，如果是自己，则弹出删除窗口，可以删除自己的回复或者评论
                if (message.getFromCircleUserId().equalsIgnoreCase(circleView.getDataSource().getProfile().getUserId())) {
                    // 评论或者回复的人是自己
                    circleView.showCommentDelWindow(CircleView.MODE_DELETE_MESSAGE);
                } else {

                    MessageEditor.getInstance().setMessageType(Message.TYPE_REPLY);
                    circleView.showEditWindow(v.getContext().getString(R.string.circle_reply) + message.getFromCircleUserName() + ":");
                }

            }
        });

    }


    // 回复的人或者被回复的人的名字设置可点击和自定义文本颜色
    private void configComment(TextView textView, Message message) {
        StringBuilder contentStringBuilder = new StringBuilder();
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        SpannableStringBuilder ssb = null;
        String contentStr = "";
        final String fromName = message.getFromCircleUserName();
        final String toName = message.getToCircleUserName();
        if (Utils.isEmpty(fromName) || Utils.isEmpty(toName)) {
            return;
        }
        textView.setTag(message);
        if (circleView != null) {
            CircleStyle circleStyle = circleView.getCircleStyle();
            if (circleStyle.getItemContentOptTextColor() != CircleStyle.INVALID) {
                colorResId = circleStyle.getItemContentOptTextColor();
            }
        }

        if (message.getMessageType() == Message.TYPE_COMMENT) {
            contentStringBuilder.append(fromName + " : ").append(message.getContent());
            contentStr = contentStringBuilder.toString();
            ssb = new SpannableStringBuilder(new SpannableString(contentStr));
            ssb.setSpan(new ClickableSpan() {

                @Override
                public void onClick(View widget) {
                    isClicked = true;

                    Message msg = (Message) widget.getTag();
                    // 设置当前所点击的评论在数据源里的Index
                    MessageEditor.getInstance().setCircleItemIndex(data.getIndex());
                    MessageEditor.getInstance().setMessageItemIndex(msg.getIndex());

                    //判断回复人或者评论人是不是自己，如果是自己，则弹出删除窗口，可以删除自己的回复或者评论
                    if (!msg.getFromCircleUserId().equalsIgnoreCase(circleView.getDataSource().getProfile().getUserId())) {
                        MessageEditor.getInstance().setMessageType(Message.TYPE_REPLY);
                        circleView.showEditWindow(widget.getContext().getString(R.string.circle_reply) + msg.getFromCircleUserName() + ":");
                    }
                }

                @Override
                public void updateDrawState(TextPaint textPaint) {
                    super.updateDrawState(textPaint);
                    textPaint.setColor(colorResId); // 设置文本颜色
                    textPaint.setUnderlineText(false); // 去掉下划线
                }

            }, contentStr.indexOf(fromName), contentStr.indexOf(fromName) + fromName.length(), 0);
        } else if (message.getMessageType() == Message.TYPE_REPLY) {
            contentStringBuilder.append(fromName + " " + textView.getContext().getString(R.string.circle_reply) + " " + toName + " : ").append(message.getContent());
            contentStr = contentStringBuilder.toString();
            ssb = new SpannableStringBuilder(new SpannableString(contentStr));

            ssb.setSpan(new ClickableSpan() {

                @Override
                public void onClick(View widget) {
                    isClicked = true;
                    Message msg = (Message) widget.getTag();
                    // 设置当前所点击的评论在数据源里的Index
                    MessageEditor.getInstance().setCircleItemIndex(data.getIndex());
                    MessageEditor.getInstance().setMessageItemIndex(msg.getIndex());

                    //判断回复人或者评论人是不是自己，如果是自己，则弹出删除窗口，可以删除自己的回复或者评论
                    if (!msg.getFromCircleUserId().equalsIgnoreCase(circleView.getDataSource().getProfile().getUserId())) {
                        MessageEditor.getInstance().setMessageType(Message.TYPE_REPLY);
                        circleView.showEditWindow(widget.getContext().getString(R.string.circle_reply) + msg.getFromCircleUserName() + ":");
                    }
                }

                @Override
                public void updateDrawState(TextPaint textPaint) {
                    super.updateDrawState(textPaint);
                    textPaint.setColor(colorResId); // 设置文本颜色
                    textPaint.setUnderlineText(false); // 去掉下划线
                }

            }, contentStr.indexOf(fromName), contentStr.indexOf(fromName) + fromName.length(), 0);

            ssb.setSpan(new ClickableSpan() {

                @Override
                public void onClick(View widget) {
                    isClicked = true;

                    Message msg = (Message) widget.getTag();
                    // 设置当前所点击的评论在数据源里的Index
                    MessageEditor.getInstance().setCircleItemIndex(data.getIndex());
                    MessageEditor.getInstance().setMessageItemIndex(msg.getIndex());

                    //判断回复人或者评论人是不是自己，如果是自己，则弹出删除窗口，可以删除自己的回复或者评论
                    if (!msg.getFromCircleUserId().equalsIgnoreCase(circleView.getDataSource().getProfile().getUserId())) {
                        MessageEditor.getInstance().setMessageType(Message.TYPE_REPLY);
                        circleView.showEditWindow(widget.getContext().getString(R.string.circle_reply) + msg.getFromCircleUserName() + ":");
                    }
                }

                @Override
                public void updateDrawState(TextPaint textPaint) {
                    super.updateDrawState(textPaint);
                    textPaint.setColor(colorResId); // 设置文本颜色
                    textPaint.setUnderlineText(false); // 去掉下划线
                }

            }, contentStr.indexOf(toName), contentStr.indexOf(toName) + toName.length(), 0);
        }
        textView.setText(ssb, TextView.BufferType.SPANNABLE);
    }

    // 获取Circle数据的item个数
    @Override
    public int getItemCount() {
        return data.getMessageList().size();
    }

    // 设置Circle列表要显示的数据源
    public void setData(Post data) {
        this.data = data;
    }
}

